package com.shanghaionstar.utils;

import android.content.Context;

import com.google.gson.Gson;
import com.onstar.cn.api.PublicService;
import com.onstar.cn.api.UsersService;
import com.onstar.cn.api.VehicleService;
import com.onstar.cn.common.exception.HttpException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.DeviceUtil;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.network.http.ResponseHandler;
import com.onstar.cn.framework.network.http.Sender;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.ChargingProfileEntity;
import com.onstar.cn.mag.CommuteScheduleEntity;
import com.onstar.cn.mag.LocalVehicleEntity;
import com.onstar.cn.mag.PlateEngineNumEntity;
import com.onstar.cn.mag.enumtype.RemoteCommandsType;
import com.onstar.cn.mag.publicService.FeedbackEntity;
import com.onstar.cn.more.response.FeedbackResponse;
import com.shanghaionstar.mro.MrOActivity;

import java.util.LinkedHashMap;

/**
 * Created by kai zhang on 2015/10/9.
 */
public class RequestUtil {
    private static class RequestUtilInner {
        static RequestUtil requestUtil = new RequestUtil();
    }

    private RequestUtil() {
    }

    public final static RequestUtil getInstance() {
        return RequestUtilInner.requestUtil;
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public void getFeedback(String content, ResponseHandler responseHandler, Context context, boolean isMro) {
        PublicService service = new PublicService(
                Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        FeedbackEntity entity = new FeedbackEntity();
        String appVersion = new DeviceUtil(context).getAppVersion();
        entity.setAppVersion(appVersion);
        entity.setAccountID(ApplicationUtil.getInstance().getAccountId());//accountid
        entity.setMobile(ApplicationUtil.getInstance().getMobile());
        entity.setEmail(ApplicationUtil.getInstance().getEmail());
        entity.setIdpID(ApplicationUtil.getInstance().getIdpID());
        entity.setComments(content);
        if (ApplicationUtil.getInstance().getCurrentVehicle()!=null) {
            entity.setVin(ApplicationUtil.getInstance().getCurrentVehicle().getVin());
        }
        requestEntity = service.feedback(requestEntity, entity);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "getFeedback", String.class, ResponseHandler.class, Context.class,boolean.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public void getFeedback(String content, final MrOActivity.OnMroOperationCallbackListener operationCallbackListener, Context context) {
        ResponseHandler responseHandler = new ResponseHandler() {
            @Override
            public void onSuccess(String tag, String json) {
                FeedbackResponse feedbackResponse = GsonHelper.fromJson( json,FeedbackResponse.class);
//                if ("200".equals(feedbackResponse.getCode()))
                operationCallbackListener.onFeedbackCallbackListener(true);
//                else
//                    operationCallbackListener.onFeedbackCallbackListener(false);
            }

            @Override
            public void onError(String tag, HttpException e) {
                operationCallbackListener.onFeedbackCallbackListener(false);
            }
        };
        getFeedback(content, responseHandler, context,true);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public void getVehicleInfo(ResponseHandler responseHandler, Context context) {
        getVehicleInfo(responseHandler, -1, context);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public void getVehicleInfo(ResponseHandler responseHandler, int timeout, Context context) {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = service.getVehicleInfo(ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), ApplicationUtil.getInstance().getCurrentVehicle().getVin());
        LinkedHashMap<String, Object> customHeads = new LinkedHashMap<String, Object>();
        customHeads.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(customHeads);
        if (timeout > 0) {
            requestEntity.setTimeout(timeout);
        }
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "getVehicleInfo", ResponseHandler.class, int.class, Context.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public void putVehicleInfo(PlateEngineNumEntity entity, ResponseHandler responseHandler, Context context) {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = service.putVehicleInfo(entity, ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), ApplicationUtil.getInstance().getCurrentVehicle().getVin());
        LinkedHashMap<String, Object> customHeads = new LinkedHashMap<String, Object>();
        customHeads.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(customHeads);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "putVehicleInfo", PlateEngineNumEntity.class, ResponseHandler.class, Context.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public void getChargingProfile(ResponseHandler responseHandler, Context context) {
        VehicleService service = new VehicleService(
                Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = service.commands(null, RemoteCommandsType.COMMAND_GETCHARGINGPROFILE, ApplicationUtil.getInstance().getCurrentVehicle().getVin(),"");
        LinkedHashMap<String, Object> customHeads = new LinkedHashMap<String, Object>();
        customHeads.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(customHeads);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "getChargingProfile", ResponseHandler.class, Context.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public void setChargingProfile(ChargingProfileEntity entity, ResponseHandler responseHandler, Context context) {
        VehicleService service = new VehicleService(
                Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = service.commands(null, RemoteCommandsType.COMMAND_SETCHARGINGPROFILE, ApplicationUtil.getInstance().getCurrentVehicle().getVin(),GsonHelper.toJson(entity));
        LinkedHashMap<String, Object> customHeads = new LinkedHashMap<String, Object>();
        customHeads.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(customHeads);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "setChargingProfile", ChargingProfileEntity.class, ResponseHandler.class, Context.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public void getCommuteSchedule(ResponseHandler responseHandler, Context context) {
        VehicleService service = new VehicleService(
                Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = service.commands(null, RemoteCommandsType.COMMAND_GETCOMMUTESCHEDULE, ApplicationUtil.getInstance().getCurrentVehicle().getVin(),"");
        LinkedHashMap<String, Object> customHeads = new LinkedHashMap<String, Object>();
        customHeads.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        customHeads.put("Accept", "application/json");
        requestEntity.setCustomHeads(customHeads);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "getCommuteSchedule", ResponseHandler.class, Context.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.POST)
    public void setCommuteSchedule(CommuteScheduleEntity entity, ResponseHandler responseHandler, Context context) {
        VehicleService service = new VehicleService(
                Constants.CURRENT_SERVER_URL);
        String vin = ApplicationUtil.getInstance().getVin();
//        ApplicationUtil.getInstance().getCurrentVehicle()
        String entityJson = new Gson().toJson(entity);
//        		JsonUtils.toJson(entity);
        RequestEntity requestEntity = service.commands(null, RemoteCommandsType.COMMAND_SETCOMMUTESCHEDULE,
        		vin==null?"":vin, entityJson);
        LinkedHashMap<String, Object> customHeads = new LinkedHashMap<String, Object>();
        customHeads.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(customHeads);
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "setCommuteSchedule", CommuteScheduleEntity.class, ResponseHandler.class, Context.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public void localVehicleService(LocalVehicleEntity entity, ResponseHandler responseHandler, Context context) {
        UsersService service = new UsersService(
                Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = service.localVehicleService(entity, ApplicationUtil.getInstance().getIdpID(), ApplicationUtil.getInstance().getAccountId(), ApplicationUtil.getInstance().getCurrentVehicle().getVin());
        Sender sender = new Sender();
        sender.send(requestEntity, responseHandler, getClass(), "localVehicleService", LocalVehicleEntity.class, ResponseHandler.class, Context.class);
    }
}
